﻿using AutoMapper;
using Domain.Entity;
using Domain.Entity.bs;
using Domain.Entity.search;
using Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.StockService
{
    public class StockService : IStockService
    {
        private readonly IStockRepository stockRepository;
        private readonly IMapper mapper;

        public StockService(IStockRepository stockRepository,IMapper mapper)
        {
            this.stockRepository = stockRepository;
            this.mapper = mapper;
        }

        //添加库存
        public async Task<int> AddStock(AddStockDto addStockDto)
        {
            Stock stock=mapper.Map<Stock>(addStockDto);
            return await stockRepository.AddStock(stock);
        }
        //库存查询
        public async Task<FenYe<StockInventoryViewDto>> GetStockAsync(FenYe<StockInventoryViewDto> fenYe, string? name, string? unit, int? whid, string? type, bool showStopped, bool filterZeroStock)
        {
            return await stockRepository.GetStockAsync(fenYe, name, unit, whid, type, showStopped, filterZeroStock);
        }


        //查询是否已存在相同商品、仓库、库位的记录
        public async Task<Stock> GetStockByCriteria(int procid, int whid, int slotid)
        {
            return await stockRepository.GetStockByCriteria(procid, whid, slotid);
        }


        //修改库存
        public async Task<int> UpdateStock(AddStockDto addStockDto)
        {
            Stock stock=mapper.Map<Stock>(addStockDto);
            return await stockRepository.UpdateStock(stock);
        }


        //修改库存(出库)
        public async Task<int> UpdateStockJian(AddStockDto addStockDto)
        {
            Stock stock = mapper.Map<Stock>(addStockDto);
            return await stockRepository.UpdateStockJian(stock);
        }


        //通过仓库ID查
        public async Task<List<Stock>> GetStockwhid(int whid)
        {
            return await stockRepository.GetStockwhid(whid);
        }

        //库存分布
        public async Task<StockTJResult> StockDto_hTJ(string? timeType, int? page, int? pagesize)
        {
            return await stockRepository.StockDto_hTJ(timeType,page,pagesize);
        }

        public async Task<StockTotals> Gettotal()
        {
            return await stockRepository.Gettotal();
        }

        public async Task<List<StockSearch>> GetByPid(int procid)
        {
            return await stockRepository.GetByPid(procid);
        }
    }
}
